'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const serviceName = 'order'
class RouterController extends Controller {
  // 分页
  async index() {
    const { query, service } = this.ctx;
    let { pageNum, pageSize, order_no = '', status } = query;
    let page = Number(pageNum) || 1;
    let size = Number(pageSize) || 10;
    /**
     *
     *       <el-tab-pane label="全部" name="all"></el-tab-pane>
     *       <el-tab-pane label="待支付" name="wait_pay"></el-tab-pane>
     *       <el-tab-pane label="待备货" name="wait_food"></el-tab-pane>
     *       <el-tab-pane label="配送中" name="wait_received"></el-tab-pane>
              <el-tab-pane label="已完成" name="finish"></el-tab-pane>
     *       <el-tab-pane label="待处理退款" name="wait_refund"></el-tab-pane>
     *       <el-tab-pane label="已退款" name="refund"></el-tab-pane>
     * @type {{}}
     */
    const where = {

    };
    if (order_no) {
      where.order_no = { [Op.like]: `%${order_no}%` }
    }

    // 待配送
    if (status === 'wait_pay') {
      where.order_status = 0
      where.status = 1
    }
    // 待备货
    if (status === 'wait_food') {
      where.order_status = 0
        where.status = 2
    }
    // 配送中
    if (status === 'wait_received') {
        where.order_status = 0
        where.status = 3
    }
    // 已完成
    if (status === 'finish') {
      where[Op.or] = [
        { order_status: 0, status: 4 },
        { order_status: 2 },
        { order_status: 3 }
      ]
    }
    // 待处理退款
    if (status === 'wait_refund') {
        where.order_status = 1
    }
    // 已退款
    if (status === 'refund') {
        where.order_status = 2
    }
    if (status === 'fail') {
        where.order_status = 3
    }
    let list = await service[serviceName].getPage(where, page, size);
    this.ctx.success(list);
  }
  // 新增
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    await ctx.service[serviceName].create(data);
    ctx.success(data, 200);
  }
  // 修改
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const infos = ctx.request.body;
    if (!id) throw new Error('必须传递ID参数');
    if (Object.keys(infos).length === 0) throw new Error('请传递修改的内容');
    await ctx.service[serviceName].update(id, infos);
    ctx.success(id, 200);
  }
  // 删除
  async destroy() {
    const uid = Number(this.ctx.params.id);
    if (!uid) throw new Error('ID 有误');
    await this.ctx.service[serviceName].deleteById(uid);
    this.ctx.success(uid); // 删除成功返回被删除的用户ID
  }
  // 查看
  async show() {
    const { params, service } = this.ctx;
    const findItem = await service[serviceName].findOne(params.id);
    this.ctx.success(findItem);
  }
  // 所有列表
  async allList() {
    const { service } = this.ctx;
    const member_id = this.ctx.query.member_id;
    const status = this.ctx.query.status;
    let where = {};
    if (member_id) {
      where.member_id = member_id;
    }
    // 待配送
    if (status === 'wait_pay') {
      where.order_status = 0
      where.status = 1
    }
    // 待备货
    if (status === 'wait_food') {
      where.order_status = 0
      where.status = 2
    }
    // 配送中
    if (status === 'wait_received') {
      where.order_status = 0
      where.status = 3
    }
    // 已完成
    if (status === 'finish') {
      where[Op.or] = [
        { order_status: 0, status: 4 },
        { order_status: 2 },
        { order_status: 3 }
      ]
    }
    // 待处理退款
    if (status === 'wait_refund') {
      where.order_status = 1
    }
    // 已退款
    if (status === 'refund') {
      where.order_status = 2
    }
    if (status === 'fail') {
      where.order_status = 3
    }
    const list = await service[serviceName].allList(where);
    this.ctx.success(list);
  }
  async staffList() {
    const { service } = this.ctx;
    const staff_id = this.ctx.query.staff_id;
    const status = this.ctx.query.status;
    let where = {};
    // 待接单
    if (status === 'wait_food') {
      where.order_status = 0
      where.status = 2
    }
    // 配送中
    if (status === 'wait_received') {
      where.order_status = 0
      where.status = 3
    }
    // 已完成
    if (status === 'finish') {
      where[Op.or] = [
        { order_status: 0, status: 4 },
        { order_status: 2 },
        { order_status: 3 }
      ]
    }
    // 待处理退款
    if (status === 'wait_refund') {
      where.order_status = 1
    }
    // 已退款
    if (status === 'refund') {
      where.order_status = 2
    }
    if (status === 'fail') {
      where.order_status = 3
    }
    if (status !== 'wait_food') {
      if (staff_id) {
        where.staff_id = staff_id;
      } else {
        console.log('龙')
        this.ctx.success([]);
      }
    }
    const list = await service[serviceName].allList(where);
    this.ctx.success(list);
  }
  async staffAccept() {
    const { service } = this.ctx;
    const data = this.ctx.request.body;
    await service[serviceName].staffAccept(data);
    this.ctx.success(data);
  }
  async confirmOrder() {
    const { service } = this.ctx;
    const data = this.ctx.request.body;
    await service[serviceName].confirmOrder(data);
    this.ctx.success(data);
  }
}

module.exports = RouterController;
